<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="88px">
      <el-form-item label="服务器地址" prop="type">
        <el-select v-model="queryParams.serverAddress" placeholder="请选择服务器地址" class="width_120" @change="handleQuery">
          <el-option
            v-for="(item,index) in IPList"
            :key="index"
            :value="item">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="服务功能" prop="serverName">
        <el-input
          v-model="queryParams.serverName"
          placeholder="请输入服务功能"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['asset:propertyInfo:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="propertyInfoList"  @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <!-- <el-table-column label="序号" align="center" prop="id" width="80" /> -->
      <el-table-column label="服务器地址" align="center" prop="serverAddress" />
      <el-table-column label="服务功能" align="center" prop="serverName" />
      <el-table-column label="服务器版本" align="center" prop="serverVersion" />
      <el-table-column label="监控状态" align="center" prop="monitorStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.servers_status" :value="scope.row.monitorStatus"/>
        </template>
      </el-table-column>
      <!-- <el-table-column label="项目数量" align="center" prop="projectAmount" /> -->
      <!-- <el-table-column label="服务端口" align="center" prop="port" /> -->
      <!-- <el-table-column label="项目部署地址" align="center" prop="deployAddress" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="check(scope.row.id)"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['asset:propertyInfo:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="changeStatus(scope.row)"
            v-hasPermi="['asset:propertyInfo:edit']"
          >{{ scope.row.monitorStatus === '1' ? '停用' : '启用' }}</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['asset:propertyInfo:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加/修改/查看服务器资产信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="服务器地址" prop="serverAddress">
          <!-- @blur="checkServerAddressIsExist"-->
          <el-input v-model="form.serverAddress" placeholder="请输入服务器地址" :disabled="checkDisabled" />
        </el-form-item>
        <el-form-item label="服务器功能" prop="serverName">
          <el-input v-model="form.serverName" placeholder="请输入服务器功能" :disabled="checkDisabled"/>
        </el-form-item>
        <el-form-item label="服务器版本" prop="serverVersion">
          <el-input v-model="form.serverVersion" placeholder="请输入服务器版本" :disabled="checkDisabled"/>
        </el-form-item>
        <el-form-item label="项目数量" prop="projectAmount">
          <el-input v-model="form.projectAmount" placeholder="请输入项目数量" :disabled="checkDisabled"/>
        </el-form-item>
        <el-form-item label="服务端口" prop="port">
          <el-input v-model="form.port" placeholder="请输入服务端口" :disabled="checkDisabled"/>
        </el-form-item>
        <el-form-item label="项目部署地址" prop="deployAddress">
          <el-input v-model="form.deployAddress" placeholder="请输入项目部署地址" :disabled="checkDisabled"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" rows="3" cols="30" placeholder="请输入内容" :disabled="checkDisabled"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="!checkDisabled" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPropertyInfo, getPropertyInfo, delPropertyInfo, addPropertyInfo, updatePropertyInfo,checkServerAddressIsExist } from "@/api/asset/propertyInfo";

export default {
  name: "PropertyInfo",
  dicts: ['servers_status'],
  props: ['currentProject'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 服务器资产信息表格数据
      propertyInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        serverName: null,
        serverAddress: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        serverAddress: [{
          required: true,
          message: "",
          trigger: "blur"
        },
          // { min: 2, max: 20, message: '用户名称长度必须介于 2 和 20 之间', trigger: 'blur' }
        ],
        serverName: [{
          required: true,
          message: "",
          trigger: "blur"
        },
        ],
        serverVersion: [{
          required: true,
          message: "",
          trigger: "blur"
        },
        ],
   /*     projectAmount: [{
          required: true,
          message: "",
          trigger: "blur"
        },
        ],
        port: [{
          required: true,
          message: "",
          trigger: "blur"
        },
        ],
        deployAddress: [{
          required: true,
          message: "",
          trigger: "blur"
        },
        ],*/
      },
      checkDisabled:false,
      IPList: [],
      username: ''
    };
  },
  created() {
    this.getList();
   this.getIPList();
  },
  mounted() {
    this.username = this.$store.state.user.name;
    // console.log("获取到的用户名为:",this.username)
  },
  methods: {
   /* checkServerAddressIsExist(){
      checkServerAddressIsExist(this.form.serverAddress).then(res=>{
        console.log(res)
      })

    }
    ,*/
    getIPList() {
      this.queryParams.projectCode = this.currentProject;
      listPropertyInfo(this.queryParams).then(response => {
        this.IPList = response.rows.map(item => item.serverAddress);
        this.loading = false;
      })
    },
    // 改变状态
    changeStatus(row) {
      this.form = {}
      this.form.id = row.id
      if(row.monitorStatus === '1') {
        this.form.monitorStatus = '0'
      } else {
        this.form.monitorStatus = '1'
      }
      // 更新表单
      this.form.updateBy = this.username;
      updatePropertyInfo(this.form).then(response => {
        this.$modal.msgSuccess("修改状态成功");
        this.open = false;
        this.getList();
      });
    },
    // 查看
    check(id) {
      getPropertyInfo(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "查看监控服务信息";
          this.checkDisabled = true;
        });
    },
    /** 查询服务器资产信息列表 */
    getList() {
      this.loading = true;
      this.queryParams.projectCode = this.currentProject
      listPropertyInfo(this.queryParams).then(response => {
        this.propertyInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      // 重置查看状态
      this.checkDisabled = false;
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        serverCode: null,
        projectCode: null,
        serverName: null,
        serverAddress: null,
        serverVersion: null,
        monitorStatus: null,
        projectAmount: null,
        port: null,
        deployAddress: null,
        remark: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      // 下拉选择时 不使用服务功能模糊查询
      if(this.queryParams.serverAddress) {
        this.queryParams.serverName = null;
      }
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.serverAddress = null;
      this.queryParams.serverName = null;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.checkDisabled = false;
      this.reset();
      this.open = true;
      this.title = "添加服务器资产信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.checkDisabled = false;
      this.reset();
      const id = row.id || this.ids
      getPropertyInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改服务器资产信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            this.form.updateBy = this.username;
            updatePropertyInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
              this.getIPList();
            });
          } else {
            // 默认新增时状态为启用
            this.form.monitorStatus = 1;
            this.form.createBy = this.username;
            this.form.projectCode = this.currentProject
            addPropertyInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
              this.getIPList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除服务器编号为"' + ids + '"的数据项？').then(function() {
        return delPropertyInfo(ids);
      }).then(() => {
        this.getList();
        this.getIPList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('asset/propertyInfo/export', {
        ...this.queryParams
      }, `propertyInfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
